@isTest
private class SelectListComponentControllerTest {

    static testMethod void AddItem() {
		SelectListComponentController controller = new SelectListComponentController();
		controller.ListOptions = getListData();

		system.assertEquals(7, controller.AvailableOptions.size());
		analyseListData(controller.AvailableOptions, new list<integer>{0,1,2,3,4,5,6});
		
		system.assertEquals(0, controller.ChosenOptions.size());
		
		controller.SelectedAvailableOption = new string[]{'2', '5'};
		controller.Add();
		
		system.assertEquals(5, controller.AvailableOptions.size());
		analyseListData(controller.AvailableOptions, new list<integer>{0,1,3,4,6});
		
		system.assertEquals(2, controller.ChosenOptions.size());
		analyseListData(controller.ChosenOptions, new list<integer>{2,5});
		
		//Add another
		controller.SelectedAvailableOption = new string[]{'4'};
		controller.Add();
		
		system.assertEquals(4, controller.AvailableOptions.size());
		analyseListData(controller.AvailableOptions, new list<integer>{0,1,3,6});
		
//		system.assertEquals(3, controller.ChosenOptions.size());
		analyseListData(controller.ChosenOptions, new list<integer>{2,4,5});
    }

    static testMethod void RemoveItem() {
		SelectListComponentController controller = new SelectListComponentController();
		controller.ListOptions = getListData();

		controller.SelectedAvailableOption = new string[]{'0', '3', '6'};
		controller.Add();

		controller.SelectedChosenOption = new string[]{'0', '6'};
		controller.Remove();
		
		system.assertEquals(6, controller.AvailableOptions.size());
		analyseListData(controller.AvailableOptions, new list<integer>{0,1,2,4,5,6});
		
		system.assertEquals(1, controller.ChosenOptions.size());
		analyseListData(controller.ChosenOptions, new list<integer>{3});	
    }

    static testMethod void ResetList() {
		SelectListComponentController controller = new SelectListComponentController();
		controller.ListOptions = getListData();

		controller.SelectedAvailableOption = new string[]{'0', '3', '6'};
		controller.Add();

		controller.ResetList();

		system.assertEquals(7, controller.AvailableOptions.size());
		analyseListData(controller.AvailableOptions, new list<integer>{0,1,2,3,4,5,6});
		
		system.assertEquals(0, controller.ChosenOptions.size());		
    }
    
    private static void analyseListData(List<Selectoption> listData, list<integer> expectedData){
    	system.debug('***analyseListData.listData: ' + listData);
    	system.debug('***analyseListData.expectedData: ' + expectedData);

		integer i = 0;
		for(Selectoption so : listData){
			system.assertEquals(string.valueOf(expectedData.get(i++)), so.getValue());
		}
    }
    
    private static list<Selectoption> getListData(){
		list<Selectoption> listData = new list<Selectoption>();
		
        listData.add(new Selectoption('0', 'Option 0'));
        listData.add(new Selectoption('1', 'Option 1'));
        listData.add(new Selectoption('2', 'Option 2'));
        listData.add(new Selectoption('3', 'Option 3'));
        listData.add(new Selectoption('4', 'Option 4'));
        listData.add(new Selectoption('5', 'Option 5'));
        listData.add(new Selectoption('6', 'Option 6'));
            
        return listData;
    }
}